<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;

/**
 * 游客接口
 */
class UserTourists extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserTourists;

    }
    /**
     * 列表
     *
     */
    public function list()
    {
        $list = db('User_tourists')
            ->where(['user_id' => $this->auth->id])
            ->order('updatetime', 'desc')
            ->select();
        $this->success('返回成功', $list);
    }


    /**
     * 新增
     *
     */
    public function add()
    {
        $data = [
            'name'    => $this->request->post('name/s'),
            'type'    => $this->request->post('type/s','id_card'),
            'id_num'  => $this->request->post('id_num/s'),
            'user_id' => $this->auth->id,
        ];
        $rule = [
            'name'   => 'require|min:2',
            'type'   => 'require|in:id_card,passport',
            'id_num' => 'require',
        ];

        $msg = [
            'type.in' => '证件类型必须是id_card或passport'
        ];
        if ($data['type'] === 'id_card') {
            $rule['id_num'] .= '|idCard';
        }

        $validate = new \think\Validate($rule, $msg);
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        try {
            $result = $this->model->save($data);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if (!$result) {
            $this->error('保存失败');
        }
        $this->success('添加成功', ['id' => $this->model->id]);
    }


    /**
     * 编辑
     *
     */
    public function edit()
    {
        $data = [
            'id'      => $this->request->post('id/s'),
            'name'    => $this->request->post('name/s'),
            'type'    => $this->request->post('type/s','id_card'),
            'id_num'  => $this->request->post('id_num/s'),
        ];
        $rule = [
            'name'   => 'require|min:2',
            'type'   => 'require|in:id_card,passport',
            'id_num' => 'require',
        ];

        $msg = [
            'type.in' => '证件类型必须是id_card或passport'
        ];
        if ($data['type'] === 'id_card') {
            $rule['id_num'] .= '|idCard';
        }

        $validate = new \think\Validate($rule, $msg);
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        if (!$this->model->where([
                'id'      => $data['id'],
                'user_id' => $this->auth->id,
            ])->find()) {
            $this->error('数据异常');
        }
        try {
            $result = $this->model->update($data);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if (!$result) {
            $this->error('保存失败');
        }
        $this->success('保存成功');
    }


}
